package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.baidu.mapapi.model.LatLng;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.AppManager;
import com.zhuozhou.jdyc.util.log.ToastUtils;

import java.io.File;

/**
 * Created by fangzhou on 2017/1/14.
 * 地图导航dialog
 */

public class MapDialog extends Dialog implements View.OnClickListener {
    Button baiduMap;
    Button gaodeMap;
    Button tencentMap;
    Button googleMap;
    Button cancelBtn;
    private Context context;

    private LatLng latLng;
    private OnNavigationMapListener onNavigationMapListener;

    public MapDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
    }

    public MapDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public MapDialog setOnNavigationMapListener(OnNavigationMapListener onNavigationMapListener) {
        this.onNavigationMapListener = onNavigationMapListener;
        return this;
    }

    public interface OnNavigationMapListener {
        void onNavigationClick(Dialog dialog, View v, LatLng latLng, int position);
    }

    public MapDialog setLocation(LatLng latLng) {
        this.latLng = latLng;
        return this;
    }

    public MapDialog builder() {
        init();
        return this;
    }

    private void init() {
        View dialogView = LayoutInflater.from(context).inflate(R.layout.map_dialog, null);
        baiduMap = dialogView.findViewById(R.id.baiduMap);
        gaodeMap = dialogView.findViewById(R.id.gaodeMap);
        googleMap = dialogView.findViewById(R.id.googleMap);
        tencentMap = dialogView.findViewById(R.id.tencentMap);
        cancelBtn = dialogView.findViewById(R.id.cancelBtn);
        baiduMap.setOnClickListener(this);
        gaodeMap.setOnClickListener(this);
        googleMap.setOnClickListener(this);
        tencentMap.setOnClickListener(this);
        cancelBtn.setOnClickListener(v -> dismiss());

        if (mapIsAvilible(context) == 4) {
            cancelBtn.setText("没有找到可用导航软件");
        }
        setContentView(dialogView);
        Window dialogWindow = getWindow();
        if (dialogWindow == null) {
            return;
        }
        dialogWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 30;// 设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp);
    }

    private int mapIsAvilible(Context context) {
        int count = 0;
        if (!AppManager.isAvilible(context, "com.baidu.BaiduMap")) {
            baiduMap.setVisibility(View.GONE);
            count++;
        }
        if (!AppManager.isAvilible(context, "com.autonavi.minimap")) {
            gaodeMap.setVisibility(View.GONE);
            count++;
        }
//        if (!AppManager.isAvilible(context, "com.google.android.apps.maps")) {
//            googleMap.setVisibility(View.GONE);
//            count++;
//        }
        if (!isHaveTencentMap()) {
            tencentMap.setVisibility(View.GONE);
            count++;
        }
        return count;
    }

    /***
     * 是否安装腾讯地图
     * @return
     */
    public boolean isHaveTencentMap() {
        try {
            if (!new File("/data/data/" + "com.tencent.map").exists()) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        dismiss();
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.baiduMap:
                intent.setData(Uri.parse("baidumap://map/direction?destination=latlng:" + latLng.latitude + "," + latLng.longitude + "|name:目的地&coord_type=bd09ll&mode=driving"));
                context.startActivity(intent);
                break;
            case R.id.gaodeMap:
                intent.setPackage("com.autonavi.minimap");
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                DPoint destination = null;
                try {
                    destination = convertBaiduToGPS(latLng);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                intent.setData(Uri.parse("androidamap://route?sourceApplication=${GlobalUtils.getString(R.string.app_name)}&" +
                        "dlat=" + destination.getLatitude() + "&dlon=" + destination.getLongitude() + "&dname=目的地" + "&dev=0&t=0"));
                context.startActivity(intent);
                break;
            case R.id.tencentMap:
                DPoint destination1 = null;
                try {
                    destination1 = convertBaiduToGPS(latLng);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                intent.setData(Uri.parse("qqmap://map/routeplan?type=walk&to=目的地&tocoord=" + destination1.getLatitude() + "," + destination1.getLongitude() + "&policy=1&referer=myapp"));
                context.startActivity(intent);
                break;
            case R.id.googleMap:
                ToastUtils.showShort("暂不支持谷歌地图");
                break;
            default:
                break;
        }
    }

    /**
     * 百度坐标系 (BD-09) 转 火星坐标系(GCJ-02)的转换
     */
    public DPoint convertBaiduToGPS(LatLng latlng) throws Exception {
        CoordinateConverter converter = new CoordinateConverter(context);
// CoordType.GPS 待转换坐标类型
        converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待转换坐标点 DPoint类型
        DPoint dPoint = new DPoint();
        dPoint.setLatitude(latlng.latitude);
        dPoint.setLongitude(latlng.longitude);
        converter.coord(dPoint);
// 执行转换操作
        DPoint desLatLng = converter.convert();
        return desLatLng;
    }
}
